﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using System.Runtime.Serialization;
using Ooganizer.Server;

namespace Ooganizer
{
    namespace API
    {
        [Serializable]
        public class OoganizerAPI
        {
            public IEnumerable<Workspace> Workspaces
            {
                get
                {
                    OoganizerDAL.OogyDB db = new OoganizerDAL.OogyDB();

                    var workspaces =
                       from w in db.Workspaces
                       select w;

                    foreach (OoganizerDAL.Workspace wsDal in workspaces)
                    {
                        Workspace ws = new Workspace(wsDal.Id);
                        yield return ws;
                    }
                }
            }

            // public IEnumerable<> GetResolvers();
            // public IEnumerable<> GetLaunchers();

            public bool CreateWorkspace(string workspaceName)
            {
                bool bSuccess = false;

                using (OoganizerDAL.OogyDB db = new OoganizerDAL.OogyDB())
                {
                    if (db.DatabaseExists())
                    {
                        db.Log = Console.Out;                       
                        OoganizerDAL.Workspace workspaceDal = new OoganizerDAL.Workspace();
                        workspaceDal.Name = workspaceName;
                        workspaceDal.Id = Guid.NewGuid();

                        try
                        {
                            Table<OoganizerDAL.Workspace> ws = db.Workspaces;

                            ws.InsertOnSubmit(workspaceDal);
                            db.SubmitChanges();

                            bSuccess = true;
                        }
                        catch (Exception e)
                        {
                            Log.Error(string.Format("CreateWorkspace() - Creation of workspace {0} failed!", workspaceName), e);
                        }
                    }
                }

                return bSuccess;
            }

            public bool DeleteWorkspace(string workspaceName)
            {
                bool bSuccess = false;

                using (OoganizerDAL.OogyDB db = new OoganizerDAL.OogyDB())
                {
                    if (db.DatabaseExists())
                    {
                        db.Log = Console.Out;

                        try
                        {
                            // Delete the workspace record if it exists
                            db.Workspaces.DeleteOnSubmit(db.Workspaces.Where(w => w.Name == workspaceName).Single());

                            db.SubmitChanges();

                            bSuccess = true;
                        }
                        catch (Exception e)
                        {
                            Log.Error(string.Format("CreateWorkspace() - Deletion of workspace {0} failed!", workspaceName), e);
                        }
                    }
                }

                return bSuccess;
            }

            public Workspace GetWorkspace(string workspaceName)
            {
                Log.Info(string.Format("{0}() - Entering",
                                        System.Reflection.MethodBase.GetCurrentMethod().Name));

                bool bSuccess = false;
                Workspace workspace = null;

                using (OoganizerDAL.OogyDB db = new OoganizerDAL.OogyDB())
                {
                    if (db.DatabaseExists())
                    {
                        db.Log = Console.Out;

                        try
                        {
                            // Find the record if it exists
                            workspace  = new Workspace(db.Workspaces.Where(w => w.Name == workspaceName).Single().Id);

                            bSuccess = true;
                        }
                        catch (Exception e)
                        {
                            Log.Error(string.Format("{0}() - Get of workspace {1} failed!", 
                                                    System.Reflection.MethodBase.GetCurrentMethod().Name, 
                                                    workspaceName), 
                                      e);
                        }
                    }
                }

                return workspace;
            }

            #region log4net declaration

            private log4net.ILog Log
            {
                get
                {
                    if (_logWrapper == null)
                    {
                        _logWrapper = new LogServerClientWrapper(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                    }

                    return _logWrapper.Log;
                }
            }

            private LogServerClientWrapper _logWrapper;

            #endregion
        }
    }
}
